package com.abl.tomcat;

import com.abl.tomcat.http.HandlerRequest;
import com.abl.tomcat.mapper.Mapper;
import com.abl.tomcat.util.LogKit;
import com.abl.tomcat.util.Property;

import java.net.ServerSocket;
import java.net.Socket;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * 程序启动入口
 */
public class Start {
    //固定大小的线程池，可控制并发数
    //超出的线程会在队列中等待
    private final static ExecutorService threadPool = Executors.newFixedThreadPool(100);

    public static void main(String[] args) throws Exception {
        ServerSocket server = new ServerSocket(Property.getPort());
        LogKit.info("服务已经启动，端口号：" + Property.getPort() + "\n");

        //循环监听
        while (true) {
            try {
                //扫描全部Servlet
                Mapper.scanServlet();

                //获取客户端
                Socket client = server.accept();

                //执行任务
                threadPool.execute(new HandlerRequest(client));

            } catch (Exception e) {
                e.printStackTrace();
            }
        }

    }

}
